package com.yonyou.pmclouds.externalservice.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.entity.PageObject;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.CommonProgramConsts;
import com.yonyou.pmclouds.externalservice.entity.FailedExternalRecordVO;
import com.yonyou.pmclouds.externalservice.mapper.FailedExternalRecordMapper;
import com.yonyou.pmclouds.externalservice.rmiitf.FailedExternalRecordQuery;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = FailedExternalRecordQuery.class)
public class FailedExternalRecordQueryImpl implements FailedExternalRecordQuery {

    private final FailedExternalRecordMapper failedRecordMapper;

    @Autowired
    public FailedExternalRecordQueryImpl(FailedExternalRecordMapper failedRecordMapper) {
        this.failedRecordMapper = failedRecordMapper;
    }

    @Override
    public PageObject<FailedExternalRecordVO> getFailedRecordByUser(String tenantId, String userId, int pageStart, int pageSize) throws BusinessException {
        if (pageSize > CommonProgramConsts.DEFAULT_MAX_QUERY_SIZE) {
            pageSize = CommonProgramConsts.DEFAULT_MAX_QUERY_SIZE;
        }
        if (pageSize < CommonProgramConsts.DEFAULT_MIN_QUERY_SIZE) {
            pageSize = CommonProgramConsts.DEFAULT_MIN_QUERY_SIZE;
        }
        int count = failedRecordMapper.countFailedRecordByUser(tenantId, userId, FailedExternalRecordVO.SERVER_SRC_APPROVAL);
        FailedExternalRecordVO[] failedRecordVOS = failedRecordMapper.getFailedRecordByUser(tenantId, userId, FailedExternalRecordVO.SERVER_SRC_APPROVAL, pageStart, pageSize);
        return new PageObject<>(failedRecordVOS, count, pageStart);
    }

    @Override
    public int checkFailedRecord(String userId, String tenantId) throws BusinessException {
        return failedRecordMapper.countFailedRecordByUser(tenantId, userId, FailedExternalRecordVO.SERVER_SRC_APPROVAL);
    }

    @Override
    public FailedExternalRecordVO queryByPkBill(String pkBill) throws BusinessException {
        return failedRecordMapper.queryRecordByPkBill(pkBill);
    }

    @Override
    public List<FailedExternalRecordVO> queryByPkBills(List<String> pkBills) throws BusinessException {
        return failedRecordMapper.queryByPkBills(pkBills);
    }
}
